package com.wang.controller;

import com.wang.entity.UserEntity;
import com.wang.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author: Wang
 * @Data: 2022/3/24 17:21
 * @Description:
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {
    private final static Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    UserService userService;

    @RequestMapping(value = "/getCount")
    public int getCount() {
        return userService.getCount();
    }

    @RequestMapping(value = "/getAllUsers")
    public List<UserEntity> getAllUsers() {
        return userService.getAllUsers();
    }

    @GetMapping(value = "/getUserById/{id}")
    public UserEntity getUserById(@PathVariable("id") Integer id) {
        return userService.getUserById(id);
    }

    @RequestMapping(value = "/addUser")
    public String addUser() {
        log.info("时间测试",new SimpleDateFormat().format(new Date(System.currentTimeMillis())));
        UserEntity user = new UserEntity();
        user.setId(userService.maxId());
        user.setUserName("wang");
        user.setPassWord("123456");
        user.setPhone("124q32425");
        user.setEmail("1782179984@qq.com");
        //user.setCreateDay(new Date(System.currentTimeMillis()));
        try {
            userService.addUser(user);
        }
        catch (Exception e) {
            e.printStackTrace();
            return "addUser失败！！！";
        }
        return "successAdd";
    }

    @GetMapping(value = "/deleteUserById/{id}")
    public String deleteUserById(@PathVariable("id") Integer id) {
        try {
            userService.deleteUserById(id);
        }
        catch (Exception e) {
            e.printStackTrace();
            return "deleteUserById失败！！！";
        }
        return "successDelete";
    }

    @GetMapping(value = "updateUserById/{id}")
    public String updateUserById(@PathVariable("id") Integer id) {
        UserEntity user;
        user = userService.getUserById(id);
        user.setUserName("test" + id);
        try {
            userService.updateUserById(user);
        }
        catch (Exception e) {
            e.printStackTrace();
            return "updateUserById失败！！！";
        }
        return "successUpdate";
    }

}
